package com.yuyou.train.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yuyou.train.service.api.user.entity.UserMedalCategory;
import com.yuyou.train.service.api.user.entity.UserMedalItem;
import com.yuyou.train.user.service.IUserLessonProgressBehaService;
import com.yuyou.train.user.service.IUserMedalCategoryService;
import com.yuyou.train.user.service.IUserMedalItemService;
import com.yuyou.train.user.vo.resp.RespMyMedalCategory;
import com.yuyou.train.user.vo.resp.RespMyMedalItem;
import com.yuyou.train.user.vo.resp.RespUserMedalTop;
import com.yuyou.train.util.tool.BeanUtils;
import com.yuyou.train.util.tool.DefindBeanUtils;
import com.yuyou.train.util.tool.Sequence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.service.api.user.dto.UserMedalRelationDTO;
import com.yuyou.train.service.api.user.entity.UserMedalRelation;
import com.yuyou.train.user.mapper.UserMedalRelationMapper;
import com.yuyou.train.user.service.IUserMedalRelationService;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.enums.CommEnums;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class UserMedalRelationServiceImpl implements IUserMedalRelationService{

	@Autowired
	private UserMedalRelationMapper userMedalRelationMapper;

	@Autowired
	private IUserMedalCategoryService userMedalCategoryService;

	@Autowired
	private IUserMedalItemService userMedalItemService;
	
	@Autowired
	private IUserLessonProgressBehaService userLesProBehService;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return userMedalRelationMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(UserMedalRelation record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return userMedalRelationMapper.insert(record);
	}

	@Override
	public UserMedalRelation selectByPrimaryKey(String id) {
		return userMedalRelationMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UserMedalRelation record) {
		return userMedalRelationMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<UserMedalRelationDTO> findUserItemRelaPageList(
			Pagination<UserMedalRelationDTO> p) {
		return userMedalRelationMapper.findUserItemRelaPageList(p);
	}

	@Override
	public int findUserItemRelaPageCount(Pagination<UserMedalRelationDTO> p) {
		return userMedalRelationMapper.findUserItemRelaPageCount(p);
	}

	@Override
	public int countMedalByUserId(String userId) {
		return userMedalRelationMapper.countMedalByUserId(userId);
	}

	@Override
	public List<RespUserMedalTop> findUserMedalTop(String classId) {
		return userMedalRelationMapper.findUserMedalTop(classId);
	}

	@Override
	public Integer myRank(String classId, String userId) {
		Integer myRank = userMedalRelationMapper.myRank(classId, userId);
		// 本人无勋章，则排最后
		if(null == myRank){
			Integer userTotal = userMedalRelationMapper.countUserOfMedal(classId);
			myRank = userTotal + 1;
		}
		return myRank;
	}


	@Override
	public List<RespMyMedalCategory> findMyMedal(String userId) {

		List<UserMedalRelation> userMedalRelationList = userMedalRelationMapper.findUserMedalRelationByUserId(userId);
		List<String> medalItemIds = BeanUtils.toList(userMedalRelationList, "medalItem");
		Map<String, UserMedalRelation> medalItemMap = BeanUtils.toMap(userMedalRelationList, "medalItem");

		List<RespMyMedalCategory> myMedalCategoryList = new ArrayList<>();

		List<UserMedalCategory> userMedalCategoryList = userMedalCategoryService.findUserMedalCategoryList();
		for(UserMedalCategory userMedalCategory : userMedalCategoryList){
			// 勋章类别
			RespMyMedalCategory myMedalCategory = new RespMyMedalCategory();
			DefindBeanUtils.copyProperties(userMedalCategory, myMedalCategory);

			// 勋章明细
			List<RespMyMedalItem> myMedalItemList = new ArrayList<>();
			List<UserMedalItem> userMedalItemList = userMedalItemService.findItemBycategoryId(userMedalCategory.getId());
			for(UserMedalItem userMedalItem : userMedalItemList){
				RespMyMedalItem myMedalItem = new RespMyMedalItem();
				DefindBeanUtils.copyProperties(userMedalItem, myMedalItem);

				if(medalItemIds.contains(userMedalItem.getId())){
					myMedalItem.setIfAward(true);
					myMedalItem.setAwardTime(medalItemMap.get(userMedalItem.getId()).getCreateTime());
				}else{
					myMedalItem.setIfAward(false);
				}
				myMedalItemList.add(myMedalItem);
			}

			myMedalCategory.setMyMedalItemList(myMedalItemList);
			myMedalCategoryList.add(myMedalCategory);
		}
		return myMedalCategoryList;
	}

	@Override
	public UserMedalRelation selectUserMedalRelation(String userId,
			String medalItem) {
		return userMedalRelationMapper.selectUserMedalRelation(userId, medalItem);
	}

	@Override
	public ResBoolSimpleInfo<String> addUserMedalRelation(String userId,
			String medalItem) {
		UserMedalRelation rel=new UserMedalRelation();
		rel.setUserId(userId);
		rel.setMedalItem(medalItem);
		this.insert(rel);		
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	@Override
	public ResBoolSimpleInfo<UserMedalItem> addMedal(String userId,String code){
		ResBoolSimpleInfo<UserMedalItem> res=new ResBoolSimpleInfo<UserMedalItem>();
		UserMedalItem item=userMedalItemService.selectByCode(code);
		if(item.getIsUse().equals(CommEnums.isUse.no.getValue())){
			res.setSuccess(false);
			return res;
		}
		UserMedalRelation userMedalRelation=this.selectUserMedalRelation(userId, item.getId());	
		if(userMedalRelation==null){
			this.addUserMedalRelation(userId,item.getId());
			//添加积分
			userLesProBehService.addMedalPoints(userId,item);
			res.setSuccess(true);
			res.setData(item);
			return res;
		}
		res.setSuccess(false);
		return res;
		
	}
	
	
	
	
	
	
	
	
}